package cn.kai.base;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.kai.base.mapper.CustomBaseMapper;
import cn.kai.base.query.LambdaQuery;
import cn.kai.base.query.QueryHead;
import cn.kai.base.query.QueryTail;
import cn.kai.enums.BizCodeEnum;
import cn.kai.exception.BizException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 基础服务类 其他微服务中的serviceImpl 继承此类 快速获取相关信息
 * Author: cKnight
 * Date: 2024/6/11
 */
@Component
@Slf4j
public class BaseService {
    /**
     * 唯一键检查
     *
     * @param clazz       mapper class 必须继承 CustomBaseMapper
     * @param sFunction   字段表达式
     * @param value       对应值
     * @param bizCodeEnum 错误编码
     */
    public <T extends CustomBaseMapper<E>, R, E extends BaseDO> void checkUnique(Class<T> clazz, SFunction<E, R> sFunction, Object value, BizCodeEnum bizCodeEnum) {
        T bean = SpringUtil.getBean(clazz);
        if (Objects.nonNull(bean)) {
            bean.checkUnique(sFunction, value, bizCodeEnum);
        }
    }

    /** 快速获取where 条件 */
    public <T extends CustomBaseMapper<E>, E extends BaseDO> QueryTail<E> where(Class<T> clazz){
        T bean = SpringUtil.getBean(clazz);
        if (Objects.nonNull(bean)) {
            return new QueryTail<>(bean,new LambdaQuery<>());
        }
        return null;
    }

    public <T extends CustomBaseMapper<E>, E extends BaseDO> QueryTail<E> where(T t){
        return t.where();
    }


    public <T extends CustomBaseMapper<E>, E extends BaseDO> void checkUnique(QueryTail<E> where ,BizCodeEnum bizCodeEnum) {
        Long count = where.count();
        if (count > 0){
            throw new BizException(bizCodeEnum) ;
        }
    }

    public <T extends CustomBaseMapper<E>, E extends BaseDO> void checkUnique(Class<T> clazz, LambdaQueryWrapper<E> wrapper, BizCodeEnum bizCodeEnum) {
        T bean = SpringUtil.getBean(clazz);
        if (Objects.nonNull(bean)) {
            bean.checkUnique(wrapper, bizCodeEnum);
        }
    }

    public <T extends CustomBaseMapper<E>, R, E extends BaseDO> void checkUnique(Class<T> clazz, SFunction<E, R> sFunction, Object value) {
        checkUnique(clazz, sFunction, value, BizCodeEnum.UNIQUE_ERROR);
    }

    /**
     * 根据条件查看当前是否存在
     */
    public <T extends CustomBaseMapper<E>, E extends BaseDO> boolean isExist(Class<T> clazz, LambdaQueryWrapper<E> wrapper) {
        T bean = SpringUtil.getBean(clazz);
        if (Objects.nonNull(bean)) {
            return bean.isExist(wrapper);
        }
        return false;
    }

    public <T extends CustomBaseMapper<E>, E extends BaseDO> boolean isExist(QueryTail<E> where) {
        Long count = where.count();
        if (count > 0){
            return true;
        }else {
            return false;
        }
    }


    public <T extends CustomBaseMapper<E>, E extends BaseDO> QueryHead<E> select(T t) {
        return new QueryHead<>(t);
    }

    /**
     * 查询
     */
    public <T extends CustomBaseMapper<E>, E extends BaseDO> QueryHead<E> select(Class<T> clazz) {
        T bean = SpringUtil.getBean(clazz);
        if (Objects.nonNull(bean)) {
            return bean.select();
        }
        return null;
    }

    /** 开始时间与结束时间都不为空 */
    public boolean queryTimeIsNotNull(BaseQueryDto baseQueryDto){
        return Objects.nonNull(baseQueryDto.getStartTime()) && Objects.nonNull(baseQueryDto.getEndTime());
    }

    public <T> void checkFeign(JsonResult<T> result){
        if (result.getCode() != 0){
            throw new BizException(result.getCode(),result.getMsg());
        }
    }
}
